{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from pandas import DataFrame\n",
    "from sklearn.preprocessing import label_binarize\n",
    "from sklearn.utils import shuffle\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.base import ClassifierMixin\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score\n",
    "from utils import read_arff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "iris: DataFrame = shuffle(read_arff('./test1-dataset/iris.arff'))\n",
    "x = iris.iloc[:, :-1]\n",
    "y = iris.iloc[:, -1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_fold():\n",
    "    kfold = KFold(10, shuffle=True)\n",
    "    for train, test in kfold.split(x, y):\n",
    "        x_train = x.iloc[train]\n",
    "        y_train = y.iloc[train]\n",
    "        x_test = x.iloc[test]\n",
    "        y_test = y.iloc[test]\n",
    "        \n",
    "        yield x_train, y_train, x_test, y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_result(clf: ClassifierMixin):\n",
    "    accuracy = []\n",
    "    precision = []\n",
    "    recall = []\n",
    "    f1 = []\n",
    "    auc = []\n",
    "    for x_train, y_train, x_test, y_test in get_fold():\n",
    "\n",
    "        clf = clf.fit(x_train, y_train)\n",
    "\n",
    "        y_test_hot = label_binarize(y_test, classes=['Iris-sesota', 'Iris-versicolor', 'Iris-virginica'])\n",
    "        y_score = clf.predict_proba(x_test) # 预测概率\n",
    "        y_test_res = clf.predict(x_test)\n",
    "        try:\n",
    "            auc.append(roc_auc_score(y_test_hot, y_score, average='micro'))\n",
    "        except ValueError as e:\n",
    "            print(e)\n",
    "\n",
    "        accuracy.append(accuracy_score(y_test, clf.predict(x_test)))\n",
    "        precision.append(precision_score(y_test, clf.predict(x_test), average='macro'))\n",
    "        recall.append(recall_score(y_test, clf.predict(x_test), average='macro'))\n",
    "        f1.append(f1_score(y_test, clf.predict(x_test), average='macro'))\n",
    "    print('分类精度:', sum(accuracy) / len(accuracy))\n",
    "    print('查准率:', sum(precision) / len(precision))\n",
    "    print('查全率:', sum(recall) / len(recall))\n",
    "    print('F值:', sum(f1) / len(f1))\n",
    "    print('AUC值:', sum(auc) / len(auc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类精度: 0.9466666666666667\n",
      "查准率: 0.9448809523809525\n",
      "查全率: 0.9522222222222221\n",
      "F值: 0.9404517704517705\n",
      "AUC值: 0.8783710802093155\n"
     ]
    }
   ],
   "source": [
    "# 决策树\n",
    "from sklearn import tree\n",
    "\n",
    "get_result(tree.DecisionTreeClassifier())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类精度: 0.9600000000000002\n",
      "查准率: 0.9543055555555556\n",
      "查全率: 0.9717261904761905\n",
      "F值: 0.9577553927553929\n",
      "AUC值: 0.856347226516989\n"
     ]
    }
   ],
   "source": [
    "# 朴素贝叶斯\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "\n",
    "get_result(GaussianNB())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类精度: 0.9600000000000002\n",
      "查准率: 0.9613492063492064\n",
      "查全率: 0.9660317460317461\n",
      "F值: 0.961043401043401\n",
      "AUC值: 0.898431209077394\n"
     ]
    }
   ],
   "source": [
    "# 最近邻算法\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "get_result(KNeighborsClassifier())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('env': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "e6a44bc4ae6224bc4bc7b3a89841677b0124c79fe22f3d101cb1ffa8756f30f3"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
